package com.hp.authweb.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import com.hp.authweb.AuthUser;
import com.hp.authweb.bean.Result;
import com.hp.authweb.model.UserInfo;
import com.hp.authweb.service.UserService;
import com.hp.sso.bean.SocialUserInfo;

/**
 * @Description:
 * @author hp
 * @date 2018年5月20日 上午10:26:25
 */
@RestController
@RequestMapping(value="/sys")
public class UserController {
	
	@Autowired
	private ProviderSignInUtils providerSignInUtils;
	
	@Autowired
	private UserService userService;
	
	@PostMapping("/userinfo")
	@ResponseBody
    public UserInfo userinfo() {
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		return userInfo.getUser();
    }
	
	@RequestMapping(value="/queryUser/{account}")
	@ResponseBody
	public UserInfo queryUser(@PathVariable String account){
		return userService.queryUser(account);
	}
	
	
	/**
	 * 查询所有用户 根据管理部门
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/findByDepartment/{depCode}")
	@ResponseBody
	public List<UserInfo> findByDepartment(@PathVariable String depCode){
		return userService.findByDepartment(depCode);
	}
	
	/**
	 * 根据userId查询用户
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/findById/{id}")
	@ResponseBody
	public UserInfo findById(@PathVariable Long id){
		return userService.findById(id);
	}
	
	@RequestMapping(value="/saveUser/{userId}/{userName}/{roleStr}",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Result saveUser(@PathVariable Long userId, @PathVariable String userName, @PathVariable String roleStr){
		return userService.saveUser(userId,userName,roleStr);
	}
	
	@RequestMapping(value="/saveUser",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Result saveUser(@RequestBody UserInfo user){
		return userService.saveUser(user);
	}

	
	/**
	 * 三方登录相关注册
	 * @param user
	 * @param request
	 */
	@GetMapping("/banding/{userAccunt}")
	public void regist(@PathVariable String userAccunt, HttpServletRequest request) {
		//不管是注册用户还是绑定用户，都会拿到一个用户唯一标识。
		providerSignInUtils.doPostSignUp(userAccunt, new ServletWebRequest(request));
	}
	
	/**
	 * 三方登录相关获取微信/qq扫码用户信息
	 * @param request
	 * @return
	 */
	@GetMapping("/user")
	public SocialUserInfo getSocialUserInfo(HttpServletRequest request) {
		SocialUserInfo userInfo = new SocialUserInfo();
		Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
		userInfo.setProviderId(connection.getKey().getProviderId());
		userInfo.setProviderUserId(connection.getKey().getProviderUserId());
		userInfo.setNickname(connection.getDisplayName());
		userInfo.setHeadimg(connection.getImageUrl());
		return userInfo;
	}
}
